
var nieuweFoto = {};

nieuweFoto.build = function(foto) {
	Ti.include('selecteer_leerling.js');

	//Ti.Media.vibrate([0, 35]);
	   
	NFwin = Ti.UI.createWindow ({
		backgroundColor: '#fff',
		layout: 'vertical',
		navBarHidden:true,    
	});                  
	
	var header = Ti.UI.createView({
	   width: Ti.UI.FILL,
	   height: '85dip',
	   backgroundImage: '/images/header.png',
	   top: 0
	});
	var title = Ti.UI.createLabel({
	    color: '#fff',
	    font: {fontSize:'20dip' },
	    top: '18dip',
	})
    title.text = 'Voer een omschrijving in';
    
	header.add(title);
	NFwin.add(header);
	
	
	if(Ti.Platform.name != 'android')
    {
        var backButton = Ti.UI.createButton({
            title: 'Terug', 
            left: '10dip',
            top: '10dip',
            width: Ti.UI.SIZE,
            height: Ti.UI.SIZE,
            color: '#090C16',    
            visible: true
        });
        
        backButton.addEventListener('click', function() {
            NFwin.close;
        });
        
        header.add(backButton);
  	}
	    
	    var descriptionView = Ti.UI.createScrollView({
	        layout: 'vertical',
	        width: '300 dip',
	        contentHeight: 'auto',
	        contentWidth: 'auto',
	        height: Ti.UI.FILL
	    });
	    
	    
	    NFwin.add(descriptionView);
	    
	    var takenPicture = Ti.UI.createImageView({
	        top: '10dip',
	        width: '300dip',
	        image: foto,
	     });
	     
	    descriptionView.add(takenPicture);
	    
	    
	    titletf = Ti.UI.createTextField({
	       top: '10dip',
            hintText: 'Titel',
            width: '300dip',
            height: '60dip',
            borderColor: '#000000',
			borderWidth: '1',
			backgroundColor: Globals.TFBackgroundColor
	    }); 
	    
	    function TitleChangeEvent(e) {
	    	if(e.value != "") {
	    		Globals.resetTextBoxErrorColor(e.source);
	    	}
	    	else {
	    		Globals.setTextBoxErrorColor(e.source);
	    	}
	    }
	    
	    descriptionView.add(titletf);
	    
	    
	    var description = Ti.UI.createTextArea({
	    	top: '10dip',
	        height: '150dip',
	        width: '300dip',
	        hintText: 'Omschrijving',
	        borderColor: '#000000',
			borderWidth: '1',
			backgroundColor: Globals.TFBackgroundColor
	    });
	    
	    descriptionView.add(description);
	    
	    var nextButton = Ti.UI.createButton({
	      top: '10dip',
	      backgroundImage: '/images/button_bg.png',
	      right: 0,
	      width: '150dip',
	      height: '75dip',  
	      bottom: '10dip',
	      color: '#fff',
	      font: {fontSize:'18dip' },
	      title: 'Volgende'
	    });
	    
   	    descriptionView.add(nextButton); 
	    
	    nextButton.addEventListener('click', function() {
    		titletf.blur();
			description.blur();
			
			if(titletf.value != "") {
		        var titel = titletf.value;
		        var omschrijving = description.value;
		        Ti.Media.vibrate([0, 35]);
		        ////WriteFotoToFile
		       		var oarray = titel.match(/[^\/#@~`\?\.\*\|<>\^\+\(\)\[\]~:\;{},\"\'\\]/gi); 	//verwijder karakters die niet mogen
					ostring = oarray.toString();
		       		var nstitel = ostring.replace(/\s|,/g, "");
	
					var fotoname = "ptf_" + nstitel + "_" + Globals.FotosnewId + ".jpg";
					
	   				var file = Globals.getFile(fotoname);
	
					if (!file.exists()) {
						file.write(foto);
					}
				////END

				selecteerLeerling.build(fotoname, titel, omschrijving);
		       
			}
			else {
				titletf.addEventListener('change', TitleChangeEvent);
				
				Ti.UI.createAlertDialog({
				    message: 'U heeft geen titel ingevuld!',
				    title: 'Foutmelding',
				    ok: 'OK'
			  	}).show();
				Globals.setTextBoxErrorColor(titletf);
			}
	    });
	    
	    NFwin.open();
	    
}


